home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / EXAMPLES / IDRAW / MAPKEY.C < prev    next >
C/C++ Source or Header  |  1980-01-05  |  3KB  |  82 lines

  1. /*
  2.  * Copyright (c) 1987, 1988, 1989 Stanford University
  3.  *
  4.  * Permission to use, copy, modify, distribute, and sell this software and its
  5.  * documentation for any purpose is hereby granted without fee, provided
  6.  * that the above copyright notice appear in all copies and that both that
  7.  * copyright notice and this permission notice appear in supporting
  8.  * documentation, and that the name of Stanford not be used in advertising or
  9.  * publicity pertaining to distribution of the software without specific,
  10.  * written prior permission.  Stanford makes no representations about
  11.  * the suitability of this software for any purpose.  It is provided "as is"
  12.  * without express or implied warranty.
  13.  *
  14.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  15.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  16.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  17.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  18.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  19.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  20.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  21.  */
  22.  
  23. // $Header: mapkey.c,v 1.7 89/10/09 14:49:03 linton Exp $
  24. // implements class MapKey.
  25.  
  26. #include "mapkey.h"
  27. #include <ctype.h>
  28. #include <stdio.h>
  29.  
  30. // MapKey clears MapKey's array to all nils.
  31.  
  32. MapKey::MapKey () {
  33.     for (int i = 0; i <= MAXCHAR; i++) {
  34.     array[i] = nil;
  35.     }
  36. }
  37.  
  38. // Enter enters an Interactor into a slot in MapKey's array.  If the
  39. // slot doesn't exist or another Interactor already occupies it,
  40. // Enter prints a warning message.
  41.  
  42. void MapKey::Enter (Interactor* i, char c) {
  43.     if (c >= 0 && c <= MAXCHAR) {
  44.     if (array[c] == nil) {
  45.         array[c] = i;
  46.     } else {
  47.         fprintf(stderr, "MapKey: slot %d already occupied!\n", c);
  48.     }
  49.     } else {
  50.     fprintf(stderr, "MapKey: slot %d not in array!\n", c);
  51.     }
  52. }
  53.  
  54. // LookUp returns the Interactor associated with the given character
  55. // or nil if there's no Interactor or the character's out of bounds.
  56.  
  57. Interactor* MapKey::LookUp (char c) {
  58.     if (c >= 0 && c <= MAXCHAR) {
  59.     return array[c];
  60.     } else {
  61.     fprintf(stderr, "MapKey: slot %d not in array!\n", c);
  62.     return nil;
  63.     }
  64. }
  65.  
  66. // ToStr returns a printable string representing the given character.
  67. // The caller must copy the returned string because the next call of
  68. // ToStr will change it.
  69.  
  70. const char* MapKey::ToStr (char c) {
  71.     static char key[3];
  72.     if (!isprint(c)) {
  73.     c = toascii(c + 0100);
  74.     key[0] = '^';
  75.     } else {
  76.     key[0] = ' ';
  77.     }
  78.     key[1] = c;
  79.     key[2] = 0;
  80.     return key;
  81. }
  82.